#include <normalTextFeild.h>
#include <iostream>

namespace lgb_gtk
{

  NormalTextFeild::NormalTextFeild()
  {
    x = 0;
    y = 0;
    width = 200;
    height = 30;

    scrollWindow = NULL;
    buffer = NULL;
    textView = NULL;

  }

  NormalTextFeild::~NormalTextFeild()
  {
    
  }

  int NormalTextFeild::Init()
  {
    if(hasInit)
      return 1;
	scrollWindow = gtk_scrolled_window_new(NULL,NULL);
	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrollWindow),GTK_SHADOW_ETCHED_IN);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollWindow), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
	
	buffer = gtk_text_buffer_new(NULL);
	gtk_text_buffer_get_start_iter (buffer, &iter);
	
	textView = gtk_text_view_new_with_buffer(buffer);
	gtk_text_view_set_editable((GtkTextView *)textView , TRUE);
	gtk_text_view_set_wrap_mode((GtkTextView *)textView, GTK_WRAP_WORD);
	gtk_text_view_set_cursor_visible((GtkTextView *)textView , TRUE);
	
	gtk_container_add(GTK_CONTAINER(scrollWindow),textView);
	gtk_widget_show_all(scrollWindow);
	/*
	gtk_text_buffer_create_tag (buffer, "underline",
				    //"size", 25,
				    "foreground", "#e700ff",
				    "underline",PANGO_UNDERLINE_SINGLE,
				    NULL);
	*/
	gtk_text_buffer_create_tag (buffer, "normal",
				    //"size", 24,
				    "foreground", "#000000",
				    //"style", PANGO_STYLE_ITALIC,
				    NULL);
	
	gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
						  text.c_str() , -1 , "normal" ,NULL);
	goo_canvas_widget_new(canvas , scrollWindow , x , y , width , height , NULL);
	
	hasInit = true;
     
	return 0;
      }

  string NormalTextFeild::GetText()
  {
    if(hasInit)
      {
	GtkTextIter startIter;
	GtkTextIter endIter;
	gtk_text_buffer_get_start_iter(buffer , &startIter);
	gtk_text_buffer_get_end_iter(buffer , &endIter);
	text = gtk_text_buffer_get_text(buffer , &startIter, &endIter, false);
      }
    return text;
  }

  int NormalTextFeild::SetText(string newText)
  {
    text = newText;

    if(!hasInit)
      return 1;

    if("" == newText){
      gtk_text_buffer_set_text(buffer,"",0);
      gtk_text_buffer_get_start_iter (buffer, &iter);
    }else{
      gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
						text.c_str() , -1 , "normal" ,NULL);
    }
    return 0;
  }

}
